package com.rainliang.project.util; 

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class DateUtil  {
	private static Log logger = LogFactory.getLog(DateUtil.class.getName());
    
	private static SimpleDateFormat simpleDateFormat; 
	
	static {
		simpleDateFormat = new SimpleDateFormat(); 
	}
	
	/**
	 * 根据默认格式 yyyy-MM-dd HH:mm 获取格式化后的日期字符串
	 * @param date 日期参数
	 * @return
	 */
	public static String getFormatDate(Date date){
		if(date==null){
			return "";
		}
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateFormat.format(date);
	}
	
	/**
	 * 判断是否是闰年
	 * @param year 参数年
	 * @return
	 */
	public static boolean isLeapYear(int year){
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 
			return true;
		return false;
	}
	
	/**
	 * 获取参数d + days天后的日期时间
	 * @param d 原始日期时间
	 * @param days 增量，以天为单位
	 * @return Date新的日期
	 */
	public static Date getNewDateByOffsetDay(Date d, int days) {
		Calendar cal=Calendar.getInstance();
		cal.setTime(d);
		cal.set(Calendar.DATE, cal.get(Calendar.DAY_OF_MONTH)+days);
		return cal.getTime();
	}
	
	/**
	 * 获取当前日期与指定偏移天数的 日期，
	 * @param numberOfDate 正数为今天以后的日期，负数为今天以前的日期
	 * @return
	 */
	public static Date getSubtractedDate(int numberOfDate) {
        Calendar calendar = Calendar.getInstance();
        int substractedDays = numberOfDate *= -1;
        calendar.add(Calendar.DATE, substractedDays);
        java.util.Date finalDate = calendar.getTime();
		return finalDate;   
	}
	
	/**
	 * 根据给定的日期字符串获取日期
	 * @param dateString
	 * @return
	 */
	public static Date getDateByString(String dateString) {
		if(dateString == null || dateString.trim().length()==0) 
			return null; 
		Date resultDate = null; 
		try {
			resultDate = simpleDateFormat.parse(dateString.trim()); 
		} catch(java.text.ParseException pe) {
			//do nothing 
		}
		return resultDate; 
	}

	//获取几小时前的时间 start////
	/**
	 * 获 date 时间  elapsedHourValue  小时前的时间
	 * @param elapsedHourValue
	 * @return
	 */
	public static Date getSubtractedDateByElapsedHourValue(Date date ,long elapsedHourValue) {
		long elapsedTimeInMillis = elapsedHourValue * 60 * 60 * 1000; 
		long currentTimeInMillis = date.getTime();

		long previousTimeInMillis = currentTimeInMillis - elapsedTimeInMillis; 
		Date previousDate = new Date(previousTimeInMillis); 
		return previousDate; 
	}
	/**
	 * 获得当前时间  elapsedHourValue  小时前的时间
	 * @param elapsedHourValue
	 * @return
	 */
	public static Date getSubtractedDateByElapsedHourValue(long elapsedHourValue) {
		return getSubtractedDateByElapsedHourValue(Calendar.getInstance().getTime(),elapsedHourValue); 
	}
	/**
	 * 获得当前时间  elapsedHourValue  小时前的时间
	 * @param elapsedHourValue
	 * @return
	 */
	public static Date getSubtractedDateByElapsedHourValue(int elapsedHourValue) {
		return getSubtractedDateByElapsedHourValue((long)elapsedHourValue); 
	}

	/**
	 * 获 date 时间  elapsedHourValue  小时前的时间
	 * @param elapsedHourValue
	 * @return
	 */
	public static Date getSubtractedDateByElapsedHourValue(Date date ,int elapsedHourValue) {
		return getSubtractedDateByElapsedHourValue(date ,(long)elapsedHourValue); 
	}
	/**
	 * 获 date 时间  elapsedHourValue  小时前的时间
	 * @param elapsedHourValue
	 * @return
	 */
	public static Date getSubtractedDateByElapsedHourValue(double elapsedHourValue) {
		long elapsedTimeInMillis = (long)(elapsedHourValue * 60.0 * 60.0 * 1000.0); 
		long currentTimeInMillis = Calendar.getInstance().getTimeInMillis();

		long previousTimeInMillis = currentTimeInMillis - elapsedTimeInMillis; 
		Date previousDate = new Date(previousTimeInMillis); 
		return previousDate; 
	}
	//获取几小时前的时间 end////
	
	/**
	 * 获取几年前的当前时间
	 * @param elapsedYearValue
	 * @return Date
	 */
	public static Date getSubtractedDateByElapsedYearValue(int elapsedYearValue) {
		Calendar calendar = Calendar.getInstance(); 
		int currentYearValue = calendar.get(Calendar.YEAR); 
		calendar.set(Calendar.YEAR, currentYearValue-elapsedYearValue); 
		return new Date(calendar.getTimeInMillis()); 
	}

	/**
	 * 获取几年前的时间
	 * @param elapsedYearValue
	 * @return Calendar
	 */
	public static Calendar getSubtractedCalendarByElapsedYearValue(int elapsedYearValue) {
		Calendar calendar = Calendar.getInstance(); 
		int currentYearValue = calendar.get(Calendar.YEAR); 
		calendar.set(Calendar.YEAR, currentYearValue-elapsedYearValue); 
		return calendar; 
	}
	
	/**
	 * 获取一天的开始时间
	 * @return Calendar
	 */
	public static Calendar getBeginningOfDayCalendar() {
		Calendar beginningOfDayCalendar = Calendar.getInstance(); 
		beginningOfDayCalendar.set(Calendar.HOUR_OF_DAY, 0);  
		beginningOfDayCalendar.set(Calendar.MINUTE, 0);  
		beginningOfDayCalendar.set(Calendar.SECOND, 0);  
		return beginningOfDayCalendar; 
	}
	
	/**
	 * 获取一天的开始时间
	 * @return
	 */
	public static Date getBeginningOfDayDate() {
		Calendar beginningOfDayCalendar = Calendar.getInstance(); 
		beginningOfDayCalendar.set(Calendar.HOUR_OF_DAY, 0);  
		beginningOfDayCalendar.set(Calendar.MINUTE, 0);  
		beginningOfDayCalendar.set(Calendar.SECOND, 0);  
		
		long beginningOfDayInMillis = beginningOfDayCalendar.getTimeInMillis(); 
		Date beginningOfDayDate = new Date(beginningOfDayInMillis); 
		
		return beginningOfDayDate; 
	}

	/**
	 * 获取年月日指定的时间长度
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
    public static long getTimeValueByYearMonthDay(int year,int month,int day){
    	java.util.Calendar calendar = Calendar.getInstance(); 
    	calendar.set(Calendar.YEAR, year); 
    	calendar.set(Calendar.MONTH, month); 
    	calendar.set(Calendar.DAY_OF_MONTH, day); 
    	
    	return calendar.getTimeInMillis();
    }

    /**
     * 获取两个日期之间相差的天数
     * @param beforeDate
     * @param afterDate
     * @return
     */
    public static int getDifferentDaysBetweenTwoDates(Date beforeDate, Date afterDate) {
        long diffMillis = afterDate.getTime() - beforeDate.getTime();
        long diffDays = diffMillis/(24*60*60*1000);  
        return (int)diffDays; 
    }
    
    /**
     * 获取指定日期是否在一个日期区间
     * @param startDate
     * @param endDate
     * @param nonceDate
     * @return 0,在起始时间之间    1,在截止日期之后     -1,在开始时间之前
     */
    public static int getNonceTimeKind(Date startDate,Date endDate,Date nonceDate){
    	long start = startDate.getTime();
    	long end = endDate.getTime();
    	long nonce = nonceDate.getTime();
    	
    	if(nonce < start){
    		return -1;
    	}else if(nonce > end){
    		return 1;
    	}
    	return 0;
    }
	
    /**
	 * java.sql.Date ==> java.util.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.util.Date dateSql2Util(java.sql.Date date) {
		return date;
	}

	/**
	 * String ==> java.util.Date
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static java.util.Date dateString2Util(String dateStr, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(dateStr, new ParsePosition(0));
	}

	/**
	 * java.util.Date ==> String
	 * 
	 * @return
	 */
	public static String dateUtil2String(java.util.Date date, String format) {
		if(date==null) return null;
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	public static Date dateUtil2date(java.util.Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(formatter.format(date), new ParsePosition(0));
	}
	/**
	 * java.util.Date ==> java.sql.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date dateUtil2SQL(java.util.Date date) {
		return new java.sql.Date(date.getTime());
	}

	/**
	 * String ==> java.sql.Date
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static java.sql.Date dateString2SQL(String dateStr, String format) {
		java.util.Date date = dateString2Util(dateStr, format);
		return dateUtil2SQL(date);
	}
	
	/**
	 * String ==> java.sql.Timestamp
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static java.sql.Timestamp timestampString2SQL(String dateStr,
			String format) {
		java.util.Date date = dateString2Util(dateStr, format);
		if (null != date) {
			return new Timestamp(date.getTime());
		} else {
			return null;
		}
	}

	/**
	 * java.util.Date date ==> java.sql.Timestamp
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static java.sql.Timestamp timestampDate2SQL(java.util.Date date) {
		if (null != date) {
			return new Timestamp(date.getTime());
		} else {
			return null;
		}
	}

	public static int getYear() {
		return new GregorianCalendar().get(Calendar.YEAR);
	}

	public static int getMonth() {
		return new GregorianCalendar().get(Calendar.MONTH) + 1;
	}
	
	public static String getMonthOfString() {
		int month = new GregorianCalendar().get(Calendar.MONTH) + 1;
		if(month<10){
			return "0"+month;
		}
		return month+"";
	}
	
	public static int getDay() {
		return new GregorianCalendar().get(Calendar.DATE);
	}
	
	public static String getDayOfString() {
		int day = new GregorianCalendar().get(Calendar.DATE);
		if(day<10){
			return "0"+day;
		}
		return day+"";
	}

	public static int getHour() {
		return new GregorianCalendar().get(Calendar.HOUR_OF_DAY);
		//return new GregorianCalendar().get(Calendar.HOUR);
	}

	public static int getMinute() {
		return new GregorianCalendar().get(Calendar.MINUTE);
	}

	public static int getSecond() {
		return new GregorianCalendar().get(Calendar.SECOND);
	}

	public static String getDateTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date Now = new java.util.Date();
		String NDate = formatter.format(Now);
		return NDate;
	}
	public static String getDatePath(){
		 String t="/";
		 String path=getYear()+t+getMonth()+t+getDay()+t+getHour();
		 return path;
	}
	public static String getImageDatePath(){
		 String path=getYear() + "" + getMonth() + "" + getDay() + "";
		 return path;
	}
	
	public static String getCurrentDate(){
		String t="-";
		String date=getYear()+t+getMonthOfString()+t+getDayOfString();
		return date;
	}
	
	public static long getNumberOfDays(Date date){
		long dateNumber=0l;
		if(date==null){
			return 0l;
		}
		Calendar beginningOfDayCalendar = Calendar.getInstance();
		beginningOfDayCalendar.setTime(new Date(System.currentTimeMillis()));
		beginningOfDayCalendar.set(Calendar.HOUR_OF_DAY, 0);  
		beginningOfDayCalendar.set(Calendar.MINUTE, 0);  
		beginningOfDayCalendar.set(Calendar.SECOND, 0);  
		
		long beginningOfDayInMillis = beginningOfDayCalendar.getTimeInMillis(); 
		beginningOfDayCalendar.setTime(date);
		beginningOfDayCalendar.set(Calendar.HOUR_OF_DAY, 0);  
		beginningOfDayCalendar.set(Calendar.MINUTE, 0);  
		beginningOfDayCalendar.set(Calendar.SECOND, 0);
		long dateMillis = beginningOfDayCalendar.getTimeInMillis(); 
		//Date now=new Date(System.currentTimeMillis());
		//dateNumber=now.getTime()-date.getTime();
		dateNumber=beginningOfDayInMillis-dateMillis;
		dateNumber=dateNumber/(1000*60*60*24);
		if(dateNumber<0){
			dateNumber=0;
		}
		return dateNumber;
	}
	public static String getChinaDate(Date date){
		String s="";
		Long temp;
		long days=getNumberOfDays(date);
		
		if((days/365)>=1l){
			temp=days/365;
			s=temp.toString();
			s= s + "年前";
		}else if((days/31)>=1l){
			temp=days/31;
			s=temp.toString();
			s= s + "月前";
		}else if((days/7)>=1l){
			temp=days/7;
			s = s + "周前";
		}else {
			temp=days;
			if(temp==0){
				s="今天";
			}else{
				s=temp.toString();
				s=s + "天前";
			}
		}
		return s;
	}
	public static Date getDateByString(String dateStr, String format){
		java.text.SimpleDateFormat df=new java.text.SimpleDateFormat(format); 
		Date d1;
		try {
			d1 = df.parse(dateStr);
		} catch (ParseException e) {
			logger.info(e.getMessage());
			return null;
		} 
		return d1;
	}
	
    public static Date[] getThisWeekStartAndEnd(int difDays,Date baseDate){
    	Date[] startEnd=new Date[2];
    	Date startTime,endTime;
    	Calendar start_date=Calendar.getInstance();
    	if(baseDate!=null)
    		start_date.setTime(baseDate);
    	start_date.add(Calendar.DAY_OF_YEAR, difDays);
		start_date.add(Calendar.DAY_OF_WEEK, -1);
	   // logger.info(start_date.getTime().toLocaleString());
		start_date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		start_date.set(Calendar.HOUR_OF_DAY, 0);
		start_date.set(Calendar.MINUTE, 0);
		start_date.set(Calendar.SECOND,0);
		startTime=start_date.getTime();
		start_date.add(Calendar.DAY_OF_MONTH, 6);
		start_date.set(Calendar.HOUR_OF_DAY, 11);
		start_date.set(Calendar.MINUTE, 59);
		start_date.set(Calendar.SECOND, 59);
		endTime=start_date.getTime();
    	startEnd[0]=startTime;
    	startEnd[1]=endTime;
    	return startEnd;
    	
    }
    public static String getPeriodOfTime(){
    	if(DateUtil.getHour()>4&&DateUtil.getHour()<=11){
    		return "早上好";
    	}else if(DateUtil.getHour()>11&&DateUtil.getHour()<=13){
    		return "中午好";
    	}else if(DateUtil.getHour()>13&&DateUtil.getHour()<=18){
    		return "下午好";
    	}else{
    		return "晚上好";
    	}
    }
    public static Date getDateBetweenTwoDate(Date startDate,Date endDate){
    	Calendar beginningOfDayCalendar = Calendar.getInstance();
    	Calendar endningOfDayCalendar = Calendar.getInstance(); 
		beginningOfDayCalendar.setTime(startDate);	
		endningOfDayCalendar.setTime(endDate);
		long beginM = beginningOfDayCalendar.getTimeInMillis();
		long endM = endningOfDayCalendar.getTimeInMillis(); 
		long m=beginM;
		long difM=endM-beginM;
		if(difM<=0){
			return new Date(beginM);
		}
		
		Random r=new Random();
		long rM=r.nextLong();
		if(rM<0){
			rM=-1l*rM;
		}
		rM=rM%difM;
		m+=rM;
		if(m<beginM){
			m=beginM;
		}
		if(m>endM){
			m=endM;
		}
		Date date=new Date(m);
		
		return date;
    }
   
    
    public static Date getDate(){
		Calendar beginningOfDayCalendar = Calendar.getInstance(); 
		beginningOfDayCalendar.set(Calendar.HOUR_OF_DAY, 0);  
		beginningOfDayCalendar.set(Calendar.MINUTE, 0);  
		beginningOfDayCalendar.set(Calendar.SECOND, 0);  
		beginningOfDayCalendar.set(Calendar.MILLISECOND, 0); 
		long beginningOfDayInMillis = beginningOfDayCalendar.getTimeInMillis(); 
		Date beforeDate = new Date(beginningOfDayInMillis);
		return beforeDate;
	}
    
    /**
     * 获得据今天dateNum间隔的时间
     * @param date			基准日期
     * @param dateNum		时间间隔。
     * @param type			dateNum代表的类型。如Calendar.SECOND代表秒, Calendar.DATE代表天
     * @return				异常情况返回NULL
     */
    public static Date getDate(Date date, int dateNum, int type){
    	try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(type, dateNum);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
    }
    public static boolean getDateDispatchToHour(Date start,Date end){
    	long createDate = start.getTime();
		long currentDate = end.getTime();
		long result = currentDate - createDate;
		int date = (int)(result/(60*1000));
		boolean falg=false;
		if(date<=0){
			falg=true;
		}
		if(date>=60){
			date = (int)(date/60);
			 
			if(date>=24){
				date = (int)(date/24);
				if(date<=2){
					falg=true;
				}
			}else{
				falg=true;
			}
		} else{
			falg=true;
		}
    	return falg;
    }
    
    
    public static String getDateDispatch(Date start,Date end){
    	long createDate = start.getTime();
		long currentDate = end.getTime();
		long result = currentDate - createDate;
		int date = (int)(result/(60*1000));
		String str = date+"<span>分钟前</span>";
		if(date<=0){
			str = "<span>1分钟前</span>";
		}
		if(date>=60){
			date = (int)(date/60);
			str = date+"<span>小时前</span>";
			if(date>=24){
				date = (int)(date/24);
				str = date+"<span>天前</span>";
				if(date>=30){
					date = (int)(date/30);
					str = date+"<span>个月前</span>";
				}
			}
		} 
    	return str;
    }
    
    public static String getDateDispatchByIndexPage(Date start,Date end){
    	long createDate = start.getTime();
		long currentDate = end.getTime();
		long result = currentDate - createDate;
		int date = (int)(result/(60*1000));
		String str = date+"<span>分钟前</span>登录过";
		if(date<=0){
			str = "<span>1分钟前</span>登录过";
		}
		if(date>=60){
			date = (int)(date/60);
			str = date+"<span>小时前</span>登录过";
			if(date>=24){
				date = (int)(date/24);
				str = date+"<span>天前</span>登录过";
				if(date>5){
					str = "";
				}
			}
		} 
    	return str;
    }
    public static String getDateStr(Date start,Date end){
    	long createDate = start.getTime();
		long currentDate = end.getTime();
		long result = currentDate - createDate;
		int date = (int)(result/(60*1000));
		String str = date+"分钟前";
		if(date<=0){
			str = "1分钟前";
		}
		if(date>=60){
			date = (int)(date/60);
			str = date+"小时前";
			if(date>=24){
				date = (int)(date/24);
				str = date+"天前";
				if(date >= 7){
					date = (int)(date/7);
					str = date+"周前";
					if(date >= 4){
						str = dateUtil2String(start, "yyyy-MM-dd");
					}
				}
			}
		} 
    	return str;
    }
    
    
    public static long getDateDispatchNowToCurrentDayEnd() {
      	Calendar cal =  Calendar.getInstance();
	    Date date = cal.getTime();
	    try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(date));
		} catch (ParseException e) {
			return 24*60*60*1000L;
		}
	    cal.setTime(date);
	    cal.add(Calendar.DAY_OF_MONTH,1);
	    cal.add(Calendar.SECOND,-1);
	    date = cal.getTime();
	    
	    Date cur_date = new Date(System.currentTimeMillis()); 
	    return date.getTime() - cur_date.getTime();
    }
    public static String getTeamTaoLunDate(Date date){
    	long time = (System.currentTimeMillis() - date.getTime());
    	if(time < 60*1000l){
    		return time/1000 +"秒前";
    	}else if(time < 60*60*1000l){
    		return time/(1000*60)+"分钟前";
    	}else if(time < 24*60*60*1000l){
    		return dateUtil2String(date, "HH:mm");
    	}else if(time < 30*24*60*60*1000l){
    		return dateUtil2String(date, "MM-dd");
    	}else {
    		return dateUtil2String(date, "yyyy-MM");
    	}
    	
    }
    
    /**
	 * 获取今天日期以前，或以后的时间
	 * 如果是正数则是以后的时间，如果是负数则是以前的时间
	 */
    public static Date getDateInHourAgo(Date date,int hourNum){
         Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 cal.add(Calendar.HOUR, hourNum);
		 return cal.getTime();
	}
    
    /**
	 * 获取当前日期是星期几
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 */
    public static int getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w <= 0)
            w = 7;

        return w;
    }
    
    /**
     * date 日期加上，或减去几天
     * @param date
     * @param dateNum
     * @return
     */
    public static Date getDateInDayAgo(int dateNum){
		 Calendar cal = Calendar.getInstance();
		 cal.add(Calendar.DATE, dateNum);
		 return cal.getTime();
	}
    
    
    /**
     * 连个日期相差的小时数
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getHour(Date startDate,Date endDate){
    	long totalDate = 0;   
 	    Calendar calendar = Calendar.getInstance();   
 	    calendar.setTime(startDate);   
 	    long timestart = calendar.getTimeInMillis();   
 	    calendar.setTime(endDate);   
 	    long timeend = calendar.getTimeInMillis();   
 	    totalDate = Math.abs((timeend - timestart))/(1000*60*60);   
 	    return totalDate;
    }
    /**
     * 传进日期是当年的第几周
     * @param date
     * @return
     */
    public static int getWeek(Date date){
    	Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c.get(Calendar.WEEK_OF_YEAR);
    }
    
    /**
     * date 日期加上，或减去几天
     * @param date
     * @param dateNum
     * @return
     */
    public static Date getDateInDayAgo(Date date,int dateNum){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 cal.add(Calendar.DATE, dateNum);
		 return cal.getTime();
	}
    /**
     * 获取 date 日期加上  或  减去几月
     * @param date
     * @param monthNum
     * @return
     */
    public static Date getDateInMonthAgo(Date date,int monthNum){
    	Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 cal.add(Calendar.MONTH, monthNum);
		 return cal.getTime();
    }
    
    public static Date getDateInMonthAgo(int monthNum){
		 return getDateInMonthAgo(new Date(), monthNum);
    }
    
    private static long fromDateStringToLong(String inVal) { //此方法计算时间毫秒
    	  Date date = null;   //定义时间类型       
    	  SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	  try { 
    	  date = inputFormat.parse(inVal); //将字符型转换成日期型
    	  } catch (Exception e) { 
    	  e.printStackTrace(); 
    	  } 
    	  return date.getTime();   //返回毫秒数
    } 


    
    public static long getMillisecond(String val1,String val2){
    	 long startT=fromDateStringToLong(val1); //定义上机时间
    	 long endT=fromDateStringToLong(val2);  //定义下机时间

    	 return startT-endT;
    }
    
    //根据给定日期，返回该日期所在星期内的开始时间与结束时间
    public static String[] getMonAndSun(Date date){
    	String[] monAndSun={"",""};
    	if(date==null)return monAndSun;
    	
    	int d=DateUtil.getWeekOfDate(date);
    	Date mon=DateUtil.getDateInDayAgo(date, 1-d);
    	Date sun=DateUtil.getDateInDayAgo(date, 7-d);
    	monAndSun[0]=DateUtil.dateUtil2String(mon, "yyyy-MM-dd")+" 00:00:00";
    	monAndSun[1]=DateUtil.dateUtil2String(sun, "yyyy-MM-dd")+" 59:59:59";
    	return monAndSun;
    }
    
    //返回给定时间的下下周一
    public static Date getLastDate(Date now){
		if(now == null) now = new Date();
		Date lastDate = null;
		int d = DateUtil.getWeekOfDate(now);
		d = 15 - d;
		lastDate = DateUtil.getDateInDayAgo(now, d);
		return lastDate;
	}
    
    
    public static void main(String[] args) throws ParseException {
    	
    	System.out.println(DateUtil.getSubtractedDateByElapsedHourValue(24));
    	
//    	Date[] dates = getThisWeekStartAndEnd(1, new Date());
//    	System.out.println(dates[0]);
//    	System.out.println(dates[1]);
//    	System.out.println(getMillisecond("2005-03-10 14:52:23", "2005-03-03 14:52:23"));
//    	System.out.println(getDateInMonthAgo(new Date(), 1).toLocaleString());
//    	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd "+"14:52:23");
//		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd "+"15:52:23");
//		String d1 = sdf1.format(DateUtil.getDateInDayAgo(-1));
//		String d2 = sdf2.format(new Date());
//    	System.out.println(d1 +"===="+d2);
//    	Date d = DateUtil.getDateByString(d1);
//    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    	
//    	System.out.println(sdf.parse(d1)+"======"+sdf.parse(d2));
    	//		Date d1 = DateUtil.dateString2Util(now + " 19:00:00", "yyy-MM-dd HH:mm:ss");
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 22:00:00");
//		String begin = sdf.format(d2);
//		logger.error(d2.toString() +"\t" + d1.toString()+"--------------------");
    }
}
